package com.bh.ly.oa.activity;

import com.bh.ly.oa.BaseActivity;
import com.bh.ly.oa.R;
import com.bh.ly.oa.util.Config;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class SettingActivity extends BaseActivity{
	
	private EditText uid, upwd,appname;
	private CheckBox checkBox;
	private Button btnLogin;
	private boolean isRemeber;
	SharedPreferences sharedPreferences;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_view);
		ImageButton button = (ImageButton)findViewById(R.id.back_button);
		button.setVisibility(View.VISIBLE);
		button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				finish();
			}
		});
		
		sharedPreferences = getSharedPreferences("Account", 0);
		checkBox = (CheckBox) findViewById(R.id.check_box);
		btnLogin = (Button) findViewById(R.id.forward_button);
		uid = (EditText) findViewById(R.id.login_uid);
		uid.setText(sharedPreferences.getString("address", ""));
		upwd =(EditText) findViewById(R.id.login_pwd);
		upwd.setText(sharedPreferences.getString("portel", ""));
		appname=(EditText) findViewById(R.id.apcitationname);
		appname.setText(sharedPreferences.getString("appname", ""));
		
		if(verificationLogin())
		{
			checkBox.setChecked(true);
		}
		
		
		btnLogin.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				if(verificationLogin())
				{
					forwordtologin();
				}
				
			}

		});
	}
	
	private void forwordtologin() {
		Editor editor = sharedPreferences.edit();
		if(checkBox.isChecked())
		{
			editor.putString("address",uid.getText().toString());
			editor.putString("portel",upwd.getText().toString());
			editor.putString("appname",appname.getText().toString());
			editor.commit();
		}else
		{
			editor.clear();
			editor.commit();
		}
		editor = null;
		Intent intent=new Intent(SettingActivity.this,LoginActivity.class);
		startActivity(intent);
	}
	
	private boolean verificationLogin() {
		if(uid.getText()==null||uid.getText().length()<=0)
		{
			
			Toast.makeText(this, "请输入服务器Ip", 3).show();
			
			return false;
		}
		
		if(upwd.getText()==null||upwd.getText().length()<=0)
		{
			Toast.makeText(this, "请输入端口号", 3).show();
			
			return false;
		}
		
		return true;
	}
}
